<template>
  <div class="app-container">
    <el-form
      v-show="show"
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
    >
      <el-form-item>
        <el-input v-model="formInline.year" placeholder="请输入年份" />
      </el-form-item>
      <el-form-item>
        <el-input v-model="formInline.month" placeholder="请输入月份" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="onSubmit">
          搜索
        </el-button>
        <!-- <el-button type="primary" icon="Download" @click="exportExcelFormServe">导出报表</el-button> -->
      </el-form-item>
    </el-form>

    <FunNavigation
      :isAdd="false"
      @showFn="show = !show"
      @refreshFn="listFn"
    ></FunNavigation>

    <el-table
      border
      header-cell-class-name="tableBackground"
      :data="list"
      stripe
      style="width: 100%"
    >
      <el-table-column width="80" prop="jobNumber" label="日期">
        <template #default="scope">
          {{ scope.row.year }}-{{ scope.row.month }}
        </template>
      </el-table-column>
      <el-table-column width="80" prop="name" label="姓名" />
      <el-table-column prop="jobNumber" label="工号" />
      <el-table-column width="100" prop="attendancePay" label="基础工资">
        <template #default="scope">
          {{
            (
              Number(scope.row.attendancePay) - Number(scope.row.levelSalary)
            ).toFixed(2)
          }}
        </template>
      </el-table-column>
      <el-table-column
        width="100"
        prop="levelSalary"
        label="职务工资"
      ></el-table-column>
      <el-table-column prop="sb" label="社保扣除" />
      <el-table-column prop="gjj" label="公积金扣除" />
      <el-table-column prop="deductionLeave" label="病假扣除" />
      <el-table-column prop="deductionLeave2" label="事假扣除" />
      <el-table-column width="130" prop="税前其他增减" label="税前其他增减" />
      <el-table-column width="140" prop="personTaxed" label="本期个人所得税" />
      <el-table-column prop="停车费" label="代扣停车费" />
      <el-table-column prop="餐费" label="代扣餐费" />
      <el-table-column width="130" prop="税后其他增减" label="税后其他增减" />
      <el-table-column prop="monthPayed" label="实发工资" />
      <!-- <el-table-column width="180" prop="addtime" label="添加时间">
        <template #default="scope">
          {{ parseTime(new Date(scope.row.createdAt).getTime()) }}
        </template>
      </el-table-column> -->
      <!-- <el-table-column fixed="right" prop="address" label="操作">
                <template #default="scope">
                    <el-button icon="Edit" @click="$router.push({ name: 'wagesEdit', query: { id: scope.row.id } })"
                        type="primary">查看</el-button>
                </template>

            </el-table-column> -->
    </el-table>
    <div class="pages">
      <el-pagination
        background
        v-model:current-page="formInline.page"
        v-model:page-size="formInline.pageSize"
        layout=" total,sizes, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>
export default {
  name: 'userList',
}
</script>
<script setup>
import {
  postAttendanceList,
  postAddattendance,
  putAttendanceCheckoutMany,
} from '@/api/api'
import FunNavigation from '@/components/funNavigation/funNavigation.vue'
import importButton from '@/components/importButton/importButton.vue'
import { parseTime } from '@/utils'
import { Search, Refresh } from '@element-plus/icons-vue'
import { onMounted, reactive, ref, computed } from 'vue'
import { permission } from '@/hooks/permission'
import { exportExcel } from '@/utils/excel'
import { useAccount } from '@/pinia/modules/account'
onMounted(() => {
  listFn()
})
const show = ref(true)
const formInline = reactive({
  page: 1,
  pageSize: 10,
  search: '',
  jobNumber: '',
  status: true,
  mobile: '',
  idCard: '',
  year: '',
  month: '',
  orderBy: {
    year: 'asc',
    month: 'asc',
  },
})

const list = ref([])
const total = ref(0)
const handleSizeChange = val => {
  formInline.pageSize = val
  listFn()
}

const handleCurrentChange = val => {
  formInline.page = val
  listFn()
}

async function listFn() {
  const { userinfo } = useAccount()
  const { data } = await postAttendanceList({
    ...formInline,
    jobNumber: userinfo.jobNumber,
  })
  data.list.map(item => {
    //本月专项扣除
    const monthDeductionList = {}
    item.staffDeduction.map(items => {
      if (items.status == 1) {
        monthDeductionList[items.name] = items.amount
      } else if (items.status == 5) {
        item[items.name] = items.amount
      }
    })
    item.sb = (
      Number(item.monthDeduction) - Number(monthDeductionList['住房公积金'])
    ).toFixed(2)
    item.gjj = monthDeductionList['住房公积金']
    item['税前其他增减'] = (
      Number(item.monthIncomeAdd) -
      Number(item.monthIncomeMinus) +
      Number(item.welfare)
    ).toFixed(2)
    item['税后其他增减'] = (
      Number(item.monthIncomeAdd2) - Number(item.monthIncomeMinus2)
    ).toFixed(2)
  })
  list.value = data.list
  total.value = data.total
}

const onSubmit = () => {
  formInline.page = 1
  listFn()
}
async function checkoutFn(id) {
  await putAttendanceCheckoutMany(id)
  listFn()
}
async function importExcelFormServe() {
  const formData = new FormData()
  formData.append('file', file)
  // const { data } = await importExcel(formData)
  // listFn()
}
async function exportList() {
  const { data } = await postAdminstaff({
    ...formInline,
    page: 1,
    pageSize: 10000,
  })
  return data.dataList
}
async function deleteFn(id) {
  await staffDelete(id)
  listFn()
}

async function exportExcelFormServe() {
  let list = await exportList()

  let dataList = list.map(item => {
    return {
      username: item.username,
      parentname: item.parentname,
      mobile: item.mobile,
      userTypeName: item.userTypeName,
      levelName: item.levelName,
      availablebalance: item.availablebalance,
      companyName: item.companyName,
      statusName: item.statusName,
      addtime: parseTime(new Date(item.addtime).getTime(), '{y}-{m}-{d}'),
    }
  })
  exportExcel(dataList, '用户列表', 'ExcelUserTitle', 'sheetName')
}

async function onImportExcelForm(e) {
  e.map(item => {
    let obj = item['基础信息']
    item['name'] = obj['姓名']
    item['jobNumber'] = obj['工号']
    item['year'] = obj['日期'].toString().substring(0, 4)
    item['month'] = obj['日期'].toString().substring(4, 6)
    item['attendanceDays'] = obj['当月出勤天数']
    item['sickLeave'] = obj['病假']
    item['businessLeave'] = obj['事假']
    delete item['基础信息']
  })
  let data = await postAddattendance(e)
  console.log(data)
}
</script>

<style>
.tableBackground {
  background: red;
}
</style>
